//
//  PhotoViewController.m
//  项目二
//
//  Created by rimi on 16/5/16.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

/**照片*/
@property(nonatomic,strong)UIImageView *imageView;
/**UIbutton*/
@property(nonatomic,strong)UIButton *button;
@end

@implementation PhotoViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"拍照、相册";
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initiInterface];
}

-(void)initiInterface{
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.button];
}

#pragma mark - GET
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.bounds =CRM(0, 0, CGRectGetWidth(self.view.bounds) *0.8, CGRectGetWidth(self.view.bounds)*0.8);
        _imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), 250);
        _imageView.backgroundColor = [UIColor lightGrayColor];
    }
    return _imageView;
}

-(UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.bounds = CRM(0, 0, 80, 30);
        _button.center =CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) + 200);
        [_button setTitle:@"照片" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
#pragma mark - UIButton
-(void)buttonTap:(UIButton *)sender{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    /**防止循环引用*/
    __weak typeof(self)weakSelf = self;
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        [weakSelf selectAlbum];
    }];
    
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        [weakSelf takePhoto];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:albumAction];
    [alertController addAction:takePhotoAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - UIAlertAction
/**相册选择照片*/
-(void)selectAlbum{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    /**picker 的资源类型*/
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    /**是否编辑照片*/
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}
/**拍照*/
-(void)takePhoto{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"该设备不支持拍照，请更换你的手机");
        return;
    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    /**picker的资源类型*/
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    /**是否编辑照片*/
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@",info);
    _imageView.image = info[@"UIImagePickerControllerEditedImage"];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
